import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @ClassName: twoSum
 * @Description: TODO
 * @Author WN
 * @Date 2021/7/13 14:40
 * @Version V1.0
 */
public class twoSum {
  int[] twosum(int[] nums,int target){
      for (int i=0;i<nums.length;i++){
          for (int j=i+1;j<nums.length;j++){
              if (nums[j]+nums[i]==target){
                  return new int[]{i,j};
              }

          }
      }
      return new int[]{-1,-1};
  }
  int[] twosum_two(int[]nums,int target){
      int n=nums.length;
      Map<Integer,Integer> index=new HashMap<>();
      for (int i=0;i<n;i++){
          index.put(nums[i],i);
      }
      for (int i=0;i<n;i++){
          int other=target-nums[i];
          if (index.containsKey(other)&&index.get(other)!=i){
              return new int[]{i,index.get(other)};
          }
      }
      return new int[]{-1,-1};
  }
}
class TwoSum{
    Map<Integer,Integer> freg=new HashMap<>();
    public void add(int number){
        freg.put(number,freg.getOrDefault(number,0)+1);
    }
    public boolean find(int value){
        for (Integer key:freg.keySet()){
            int other=value-key;
            if (other==key&&freg.get(key)>1) {
                return true;
            }
            if (other!=key&&freg.containsKey(other)){
                return true;
            }
            return false;
        }
        return false;
    }
}
class twoSumEnd{
    Set<Integer> sum=new HashSet<>();
    List<Integer> nums=new ArrayList<>();
    public void add(int number){
        for (int i: nums){
            sum.add(i+number);
        }
        nums.add(number);
    }
    public boolean find(int value){
        return sum.contains(value);
    }
}
//3.有序数组
class twoSum3{
    int[] twoSum(int[] nums,int target){
        int left=0;
        int right=nums.length-1;
        while (left<right){
            int sum=nums[left]+nums[right];
            if (sum==target){
                return new int[]{left,right};
            }else if (sum<target){
                left++;
            }else if (sum>target){
                right--;
            }
        }
        return new int[]{-1,-1};
    }

    public static void main(String[] args) {
        twoSum3 twoSum3=new twoSum3();
        int[] ints = twoSum3.twoSum(new int[]{1, 2, 3, 4, 5}, 7);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

}
